package com.zhulin.android.edoctor;

import com.zhulin.android.edoctor.fragment.FragmentCaseList;
import com.zhulin.android.edoctor.fragment.FragmentCaseListSubitem;
import com.zhulin.android.edoctor.fragment.FragmentFindPassword;
import com.zhulin.android.edoctor.fragment.FragmentLogin;
import com.zhulin.android.edoctor.fragment.FragmentLoginSelectList;
import com.zhulin.android.edoctor.fragment.FragmentMyselftCaseList;
import com.zhulin.android.edoctor.fragment.FragmentRegister;
import com.zhulin.android.edoctor.fragment.FragmentUploadEditItem;
import com.zhulin.android.edoctor.fragment.FragmentUploadSelectList;
import com.zhulin.android.edoctor.jsonobject.JOCaseInfoModel;
import com.zhulin.android.edoctor.jsonobject.OUTGetCaseList.GetCaseListModel;
import com.zhulin.android.edoctor.util.Command;
import com.zhulin.android.edoctor.util.IntentContainer;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentManager.OnBackStackChangedListener;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;

public class MainActivity extends BaseActivity {

	FragmentManager fragmentManager;
	View viewBack;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);


		fragmentManager= getSupportFragmentManager();
		fragmentManager.addOnBackStackChangedListener(onBackStackChangedListener);

		initView();
	}
	
	private OnClickListener onClickListenerBack=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			fragmentManager.popBackStack();
		}
	};

	private void initView(){
		
		viewBack=this.findViewById(R.id.btnBack);
		viewBack.setOnClickListener(onClickListenerBack);
		
		addFragmentOfCaseList();
	}
	
	private void addFragmentOfCaseList(){
		setPageTitle(Command.TITLE_SHOWCASE);
		FragmentCaseList fragmentCaseList=new FragmentCaseList();
		FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();
		fragmentTransaction.setCustomAnimations(R.anim.right_in, R.anim.right_out);
		fragmentTransaction.replace(R.id.linearLayoutBaseContentContainer, fragmentCaseList);
		fragmentTransaction.addToBackStack(MainActivity.class.getName());
		fragmentTransaction.commit();
	}
	
	private void addFragmentOfMyseftCaseList(){
		setPageTitle(Command.TITLE_MYCASE);
		FragmentMyselftCaseList mFragmentMyselftCaseList=new FragmentMyselftCaseList();
		FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();
		fragmentTransaction.setCustomAnimations(R.anim.right_in, R.anim.right_out);
		fragmentTransaction.replace(R.id.linearLayoutBaseContentContainer, mFragmentMyselftCaseList);
		fragmentTransaction.addToBackStack(MainActivity.class.getName());
		fragmentTransaction.commit();
	}
	
	public void addFragmentOfCaseDetail(GetCaseListModel joCaseInfoModel){
		FragmentCaseListSubitem fragmentCaseListSubitem=new FragmentCaseListSubitem();
		FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();
		fragmentTransaction.replace(R.id.linearLayoutBaseContentContainer, fragmentCaseListSubitem);
		fragmentTransaction.setCustomAnimations(R.anim.right_in, R.anim.right_out);
		fragmentTransaction.addToBackStack(MainActivity.class.getName());
		
		Bundle bundle=new Bundle();
		bundle.putSerializable(IntentContainer.INTENT_DATA_KEY, joCaseInfoModel);
		fragmentCaseListSubitem.setArguments(bundle);
		fragmentTransaction.commit();
		
	}
	
	public void addFragmentOfUserLogin(){
		FragmentLogin mFragmentLogin=new FragmentLogin();
		FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();
		fragmentTransaction.replace(R.id.linearLayoutBaseContentContainer, mFragmentLogin);
		fragmentTransaction.setCustomAnimations(R.anim.right_in, R.anim.right_out);
		fragmentTransaction.addToBackStack(MainActivity.class.getName());
		fragmentTransaction.commit();
	}
	
	public void addFragmentOfUserLoginSelectList(){
		FragmentLoginSelectList mFragmentLoginSelectList=new FragmentLoginSelectList();
		FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();
		//fragmentTransaction.replace(R.id.linearLayoutBaseContentContainer, mFragmentLogin);
		fragmentTransaction.setCustomAnimations(R.anim.right_in, R.anim.right_out);
		fragmentTransaction.addToBackStack(null);
		mFragmentLoginSelectList.setCancelable(true);
		mFragmentLoginSelectList.show(fragmentManager, MainActivity.class.getName()+"Dialog");
		//fragmentTransaction.commit();
		
	}
	
	public void addFragmentOfRegister(){
		FragmentRegister mFragmentRegister=new FragmentRegister();
		FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();
		fragmentTransaction.replace(R.id.linearLayoutBaseContentContainer, mFragmentRegister);
		fragmentTransaction.setCustomAnimations(R.anim.right_in, R.anim.right_out);
		fragmentTransaction.addToBackStack(MainActivity.class.getName());
		fragmentTransaction.commit();
	}
	
	/*
	 * 找回密码
	 */
	public void addFragmentOfFindPassword(){
		FragmentFindPassword mFragmentFindPassword=new FragmentFindPassword();
		FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();
		fragmentTransaction.replace(R.id.linearLayoutBaseContentContainer, mFragmentFindPassword);
		fragmentTransaction.setCustomAnimations(R.anim.right_in, R.anim.right_out);
		fragmentTransaction.addToBackStack(MainActivity.class.getName());
		fragmentTransaction.commit();
	}

	/*
	 * 上传病例列表类别选择界面
	 */
	private void addFragmentOfUploadCaseList(){
		FragmentUploadSelectList mFragmentUploadSelectList=new FragmentUploadSelectList();
		FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();
		fragmentTransaction.replace(R.id.linearLayoutBaseContentContainer, mFragmentUploadSelectList);
		fragmentTransaction.setCustomAnimations(R.anim.right_in, R.anim.right_out);
		fragmentTransaction.addToBackStack(MainActivity.class.getName());
		fragmentTransaction.commit();
	}
	
	public void addFragmentOfUploadCaseItemEdit(String typeCase){
		FragmentUploadEditItem mFragmentUploadEditItem=new FragmentUploadEditItem();
		FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();
		fragmentTransaction.replace(R.id.linearLayoutBaseContentContainer, mFragmentUploadEditItem);
		fragmentTransaction.setCustomAnimations(R.anim.right_in, R.anim.right_out);
		fragmentTransaction.addToBackStack(MainActivity.class.getName());
		Bundle bundle=new Bundle();
		bundle.putString("data", typeCase);
		mFragmentUploadEditItem.setArguments(bundle);
		fragmentTransaction.commit();
	}
	
	OnBackStackChangedListener onBackStackChangedListener=new OnBackStackChangedListener() {

		@Override
		public void onBackStackChanged() {
			if(fragmentManager.getBackStackEntryCount()==1){
				viewBack.setVisibility(View.INVISIBLE);
			}else{
				viewBack.setVisibility(View.VISIBLE);
			}
		}
	};
	
	@Override
	protected void showCaseList() {
		super.showCaseList();
		
		clearFragmentListStack();
		addFragmentOfCaseList();
	};
	
	private void clearFragmentListStack(){
		while(fragmentManager.getBackStackEntryCount()>0){
			fragmentManager.popBackStack();
			fragmentManager.executePendingTransactions();
		}
	}

	@Override
	protected void showSelftCaseList() {
		super.showSelftCaseList();
		clearFragmentListStack();
		addFragmentOfMyseftCaseList();
	}
	
	@Override
	protected void uploadCase() {
		super.uploadCase();
		//clearFragmentListStack();
		addFragmentOfUploadCaseList();
	}
}
